package per.dg.tencent.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

import java.util.ArrayList;
import java.util.List;

import per.dg.tencent.R;
import per.dg.tencent.databinding.ItemHomeFragmentBinding;
import per.dg.tencent.model.VideoModel;
import per.dg.tencent.views.recyclerview.SuspensionHeadItemdecoration;

public class HomeFragmentAdapter extends PagerAdapter {

    private static final String TAG = "HomeFragmentAdapter";

    ItemHomeFragmentBinding mItemHomeFragmentBinding;

    List<String> mStringList;

    List<View> mViewList;

    public HomeFragmentAdapter(View view, List<String> mStringList){
        mItemHomeFragmentBinding = ItemHomeFragmentBinding.bind(view);
        this.mStringList = mStringList;
    }

    public HomeFragmentAdapter(List<View> mViewList){
        this.mViewList = mViewList;
    }

    @Override
    public int getCount() {
//        return mStringList == null ? 0 : mStringList.size();
        return mViewList == null ? 0 : mViewList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        Log.e(TAG, "instantiateItem: "+position );
        switch (position){
            case 0:
                final List<VideoModel> mList = new ArrayList<>();
                mList.add(new VideoModel("banner"));
                mList.add(new VideoModel("title"));
                VideoModel huizhui = new VideoModel("huizhui");
                mList.add(huizhui);
                List<VideoModel> huizhuiList = new ArrayList<>();
                for(int i = 0; i < 7; i++){
                    huizhuiList.add(new VideoModel("redian"));
                }
                huizhui.setList(huizhuiList);


                for(int i = 0; i < 2; i++){
                    mList.add(new VideoModel("title"));
                    for(int j = 0; j < 3; j++){
                        mList.add(new VideoModel("redian"));
                    }
                }
                View view = mViewList.get(position);
                RecyclerView rv = view.findViewById(R.id.app_rv);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(container.getContext(), 2);
                gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        String type = mList.get(position).getType();
                        switch (type){
                            case "banner":
                            case "title":
                            case "huizhui":
                                return 2;
                            case "redian":
                                return 1;
                            default:
                                return 1;
                        }
                    }
                });
                rv.addItemDecoration(new SuspensionHeadItemdecoration(container.getContext(), mList));
                rv.setLayoutManager(gridLayoutManager);
                rv.setAdapter(new VideoRecyclerViewAdapter(container.getContext(), mList));



                return view;
            case 1:
                return mViewList.get(position);
        }
        return mItemHomeFragmentBinding.appLineHomeJingxuan;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View) object);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return super.getPageTitle(position);
    }
}
